<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/20
 * Time: 11:29
 */
namespace app\admin\controller;


use think\Db;
use think\Request;
class Evaluate extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('self_goods_comment')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.comment_user_id','left')
                ->join('up_self_goods g','a.comment_good_id=g.good_id','LEFT')
                ->count();// 获取总条数
            $lists = Db::name('self_goods_comment')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.comment_user_id','left')
                ->join('up_self_goods g','a.comment_good_id=g.good_id','LEFT')
                ->page($NowPage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_phone,good_name')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 详情
     * @return mixed
     */
    public function detail(){
        $car_id = input('comment_id');
        $detail = Db::table('up_self_goods_comment')
            ->where('comment_id', $car_id)
            ->find();
        if($detail['comment_image']){
            $detail['comment_image'] = explode(',',$detail['comment_image']);
        }
        $this->assign('car', $detail);
        return $this->fetch('evaluate/detail');
    }

    /**
     * 删除车辆
     * @return \think\response\Json
     */
    public function del(){
        $car_id = input('id');
        try{
            // 删除车辆
            Db::table('up_self_goods_comment')->where('comment_id', $car_id)->delete();
            // 删除类别
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    public function reply(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $comment_reply_content = trim(input('content'), ' ');
            $data = [
                'comment_reply_content' => $comment_reply_content,
                'comment_reply_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_self_goods_comment')->where('comment_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '回复成功']);
            }else{
                return json(['code' => 100, 'msg' => '回复失败']);
            }
        }
        $id = input('param.report_id');
        $detail = Db::table('up_self_goods_comment')
            ->where('comment_id', $id)
            ->find();
        if($detail['comment_image']){
            $detail['comment_image'] = explode(',',$detail['comment_image']);
        }
        $this->assign('id',$id);
        $this->assign('car',$detail);
        return $this->fetch();
    }
}